package conchmusic.conchmusicclient.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import conchmusic.conchmusicclient.R;
import conchmusic.conchmusicclient.pojo.Song;
import conchmusic.conchmusicclient.popupWindow.DownloadPopuWindow;
import conchmusic.conchmusicclient.popupWindow.OnlinePopupWindow;
import conchmusic.conchmusicclient.popupWindow.SelectPicPopupWindow;

/**
 * Created by Administrator on 2017/5/3.
 */

public class SearchAdapter extends BaseAdapter {

    private Context context;
    private List<Song> listItems;
    private LayoutInflater listContainer;
    private SelectPicPopupWindow menuWindow;
    private Activity activity;
    public SearchAdapter(Context context, List<Song> listItems) {
        this.context = context;
        listContainer = LayoutInflater.from(context);
        this.listItems = listItems;
        this.activity=(Activity)context;
    }

    public final class SearchListItemView{                //自定义控件集合
        public TextView localSongName;
        public TextView LocalSinger;
        public ImageButton imageButton;
        public ProgressBar progressBar;
    }
    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {

        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchAdapter.SearchListItemView listItemView = null;
        if (convertView == null) {
            listItemView = new SearchAdapter.SearchListItemView();
            convertView = listContainer.inflate(R.layout.search_basic, null);
            listItemView.localSongName=(TextView) convertView.findViewById(R.id.search_text_view_name);
            listItemView.LocalSinger=(TextView) convertView.findViewById(R.id.search_text_view_info);
            listItemView.imageButton=(ImageButton) convertView.findViewById(R.id.search_image_button_action);
            listItemView.progressBar=(ProgressBar)convertView.findViewById(R.id.download_progress_bar);
            convertView.setTag(listItemView);
        }else {
            listItemView = ( SearchAdapter.SearchListItemView)convertView.getTag();
        }
        List<String> artistNamesList=listItems.get(position).getArtistNames();
        String artistName="";
        for(String artist:artistNamesList){
            artistName=artistName+artist+" ";
        }
        listItemView.LocalSinger.setText(artistName);
        listItemView.localSongName.setText((String) listItems.get(position).getTitle());
        listItemView.imageButton.setImageResource(R.drawable.ic_list_item_action);
        List<Object> list=new ArrayList<>();
        list.add(position);
        list.add(listItemView.progressBar);
        listItemView.imageButton.setTag(list);
        addListener(listItemView.imageButton);
        return convertView;
    }
    public void addListener(ImageButton imageButton){
        final List<Object> list=(ArrayList) imageButton.getTag();
        final int posi=(int)list.get(0);
        final ProgressBar progressBar=(ProgressBar) list.get(1);
        imageButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("className",activity.getLocalClassName());
                        switch (activity.getLocalClassName()){
                            case "activity.SearchActivity":
                                menuWindow= new SelectPicPopupWindow(SearchAdapter.this,activity,posi,listItems);
                                menuWindow.showAtLocation(activity.findViewById(R.id.activity_search), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                                break;
                            case "activity.OnlineSearchActivity":
                                OnlinePopupWindow onlinePopupWindow= new OnlinePopupWindow(SearchAdapter.this,activity,posi,listItems,progressBar);
                                onlinePopupWindow.showAtLocation(activity.findViewById(R.id.activity_online), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                                break;
                            case "activity.DownloadActivity":
                                DownloadPopuWindow downloadPopuWindow= new DownloadPopuWindow(SearchAdapter.this,activity,posi,listItems);
                                downloadPopuWindow.showAtLocation(activity.findViewById(R.id.downloadMusic), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                                break;
                            case "activity.DownloadSearchActivity":
                                DownloadPopuWindow downloadSearchPopuWindow= new DownloadPopuWindow(SearchAdapter.this,activity,posi,listItems);
                                downloadSearchPopuWindow.showAtLocation(activity.findViewById(R.id.download_activity_search), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                                break;
                            case "activity.LocalMusicActivity":
                                menuWindow= new SelectPicPopupWindow(SearchAdapter.this,activity,posi,listItems);
                                menuWindow.showAtLocation(activity.findViewById(R.id.localMusic), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);

                        }
                    }
                }

        );
    }
}
